# -*- coding:utf-8 -*-
from rediscluster import StrictRedisCluster
from bs4 import BeautifulSoup
import requests
import json
import redis
import codecs
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

rank = 0

def getcontent(page):
    pythonul='https://fe-api.zhaopin.com/c/i/sou?start='+str(int(page)*60)+'&pageSize=60&cityId=854&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=python&kt=3&lastUrlQuery=%7B%22jl%22:%22854%22,%22kw%22:%22python%22,%22kt%22:%223%22%7D'
    url = 'https://fe-api.zhaopin.com/c/i/sou?start='+str(int(page)*60)+'&pageSize=60&cityId=854&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=java&kt=3&lastUrlQuery=%7B%22p%22:3,%22jl%22:%22854%22,%22kw%22:%22java%22,%22kt%22:%223%22%7D'
    source_code = requests.get(pythonul)
    if source_code.status_code == 200:
        # print(response.apparent_encoding)#查看编码
        # response.encoding = response.apparent_encoding
        soup = BeautifulSoup(source_code.text, 'lxml')
        return json.loads(soup.text)
    return ''

def get_json(jsonstr):
    str = jsonstr.get('data').get('results')
    for film in str:  # every 'film' is a dict
        #  rank = rank + 1  # rank
        # every_film = every_film + ' 公司名称:' + film.get('company').get('name')  # add film's name
        # every_film = every_film + ' 薪资:' + film.get('salary')  # add rating
        # every_film = every_film + ' 职位:' + film.get('jobName')  # add date
        # every_film = every_film + ' 城市:' + film.get('city').get('display') + '\r\n'  # add url
        print  film.get('salary')
        redis_cluster(' 公司名称:' + film.get('company').get('name')+ ' 薪资:' + film.get('salary')+ ' 职位:' + film.get('jobName')
                      + ' 城市:' + film.get('city').get('display')  )
    return ''

def redis_cluster(every_film):
    # redis_nodes =  [{'host':'10.100.31.209','port':30001},
    #                 {'host':'10.100.31.209','port':30002},
    #                 {'host':'10.100.31.209','port':30003},
    #                 {'host':'10.100.31.209','port':30004},
    #                 {'host':'10.100.31.209','port':30005},
    #                 {'host':'10.100.31.209','port':30006},
    #                 {'host':'10.100.31.209','port':30007}
    #                ]
    try:
        # redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
        redisconn = redis.Redis(host='localhost', port=6379, db=0)

    except Exception,e:
        print "Connect Error!"
        sys.exit(1)
    print(every_film)
    redisconn.rpush("python",every_film)

for colnum in range(0, 20):
    get_json(getcontent(colnum))



# for start in range(0, 80, 20):
#     page=1;
#     url = 'https://fe-api.zhaopin.com/c/i/sou?start=0&pageSize=60&cityId=854&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=java&kt=3&lastUrlQuery=%7B%22p%22:3,%22jl%22:%22854%22,%22kw%22:%22java%22,%22kt%22:%223%22%7D'
#
#     source_code = requests.get(url)
#     # source_code.encoding = source_code.apparent_encoding
#     soup = BeautifulSoup(source_code.text, 'lxml')
#
#     j = json.loads(soup.text)  # explain json
#     str= j.get('data').get('results')
#
#     for film in str:  # every 'film' is a dict
#         # print film
#         rank = rank + 1  # rank
#         every_film = every_film +' : Name:' + film.get('company').get('name')  # add film's name
#         every_film = every_film + ' salary:' + film.get('salary')  # add rating
#         every_film = every_film + ' jobName:' + film.get('jobName')  # add date
#         every_film = every_film + ' city:' + film.get('city').get('display') + '\r\n'  # add url
#
# print every_film
#
# folder_path = 'D:/job.txt'
#
# with codecs.open(folder_path, 'wb', 'gbk') as f:
#     f.write(every_film)
#
# print 'DONE!'
